#include <iostream>
#include "./include/mysql.h"
#include "comm.hpp"

bool InsertSql(std::string sql)
{
  MYSQL* my=mysql_init(nullptr);
  mysql_set_character_set(my,"utf8");

  if(nullptr==mysql_real_connect(my,"127.0.0.1","用户名","用户密码","test_1",3306,nullptr,0))
  {
  mysql_close(my);
    std::cerr<<"链接失败"<<std::endl;
    return false;
  }
  std::cerr<<"链接成功"<<std::endl;

  std::cerr<<sql<<std::endl;
  int ret=mysql_query(my,sql.c_str());
  if(ret)
  {
  mysql_close(my);
    std::cerr<<"添加失败"<<ret<<std::endl;
    return false;
  }
  std::cerr<<ret<<std::endl;

  std::cerr<<"添加成功"<<std::endl;

  mysql_close(my);
  return true;
}

std::string UrlDecode(const std::string& szToDecode)
{
     std::string result;
     int hex = 0;
     for (size_t i = 0; i < szToDecode.length(); ++i)
     {
         switch (szToDecode[i])
         {
         case '+':
             result += ' ';
             break;
         case '%':
             if (isxdigit(szToDecode[i + 1]) && isxdigit(szToDecode[i + 2]))
             {
                 std::string hexStr = szToDecode.substr(i + 1, 2);
                 hex = strtol(hexStr.c_str(), 0, 16);
                 //字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@]
                 //可以不经过编码直接用于URL
                 if (!((hex >= 48 && hex <= 57) || //0-9
                     (hex >=97 && hex <= 122) ||   //a-z
                     (hex >=65 && hex <= 90) ||    //A-Z
                     //一些特殊符号及保留字[$-_.+!*'(),]  [$&+,/:;=?@]
                     hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29
                     || hex == 0x2a || hex == 0x2b|| hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f
                     || hex == 0x3A || hex == 0x3B|| hex == 0x3D || hex == 0x3f || hex == 0x40 || hex == 0x5f
                     ))
                 {
                     result += char(hex);
                     i += 2;
                 }
                 else result += '%';
             }else {
                 result += '%';
             }
             break;
         default:
             result += szToDecode[i];
             break;
         }
     }
     return result;
 }

int main()
{

  std::string query_string;
  if(GetQuryString(query_string))
  {
    //name=张三&passwd=12344567
    std::cerr<<query_string<<std::endl;

    std::string tmp_name;
    std::string tmp_passwd;
    CutString(query_string,"&",tmp_name,tmp_passwd);

    std::string name;
    std::string _name;
    CutString(tmp_name,"=",_name,name);
   
    //游览器传送的中文是经过encode 加密的，所以需要decode解密
    std::string tmp =UrlDecode(name);   

    std::string passwd;
    std::string _passwd;
    CutString(tmp_passwd,"=",_passwd,passwd);

    std::string sql="insert into test_mysql(name,passwd) values(\'";
    sql+=tmp;
    sql+="\',\'";
    sql+=passwd;
    sql+="\');";
    if(InsertSql(sql)==true)
  {
    std::cout << "<html>";
            std::cout << "<head><meta charset=\"utf-8\"></head>";
            std::cout << "<body><h1>注册成功!</h1></body>";

  }
  }

  return 0;
}
